<?php


namespace App\Http\Controllers\Admin\WeekStar;

use App\Enums\RoomEnum;
use \App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;

class WeekStarHistoryController extends BaseController
{

    /**
     * @api {get} /week/star/history/list 获取周星榜前三排名
     * @apiVersion 0.1.1
     * @apiName 获取周星榜
     * @apiGroup 周星榜管理
     *
     * @apiParam {number} week_id
     * @apiError 98040  拉取礼物失败
     * @apiErrorExample 错误响应
     * {"code": 98040,"message": "errorMsg"}
     * @apiSuccessExample 成功响应
      {
      "code": 200,
      "data": {
          "2": [ //1周星榜2富豪榜3房间榜
              {
              "id": 27,
              "week_star_id": 28,
              "user_id": 547900,
              "room_id": 0,
              "type": 2,
              "rank": 1,
              "add_time": 1607156582,
              "total": 666666,
              "user_data": {
                  "user_id": 547900,
                  "nickname": "啊吧啊吧啊吧啊吧啊b"
              },
              "week_stars": {
                  "id": 28,
                  "gift_id_1": 77,
                  "gift_id_2": 78,
                  "weeks": 202049,
                  "period": 1,
                  "gift_data1": {
                  "id": 77,
                  "name": "萤火之森"
              },
              "gift_data2": {
                  "id": 78,
                  "name": "摘星揽月"
              }
              }
            },
          {
          "id": 29,
          "week_star_id": 28,
          "user_id": 547900,
          "room_id": 53,
          "type": 2,
          "rank": 2,
          "add_time": 0,
          "total": 333,
          "user_data": {
              "user_id": 547900,
              "nickname": "啊吧啊吧啊吧啊吧啊b"
          },
          "week_stars": {
              "id": 28,
              "gift_id_1": 77,
              "gift_id_2": 78,
              "weeks": 202049,
              "period": 1,
              "gift_data1": {
              "id": 77,
              "name": "萤火之森"
          },
          "gift_data2": {
              "id": 78,
              "name": "摘星揽月"
          }
          }
          }
      ]
     */

    public function getWeekRankInfoApi(Request $request)
    {
        $weekId = $request->input('week_id', 0);
        $weekList = app('repo_week_star_history')->getWeekRankInfo($weekId);
        return Response::sendData($weekList);
    }

    /**
     * @api {get} week/star/history/user/list 通过用户名查找
     * @apiVersion 0.1.1
     * @apiName 获取周星榜用户信息
     * @apiGroup 周星榜管理
     *
     * @apiParam {bool} this_week
     * @apiParam {number} type 1周星榜2富豪榜3房间榜
     * @apiParam {string} nickname
     * @apiParam {number} page
     * @apiParam {number} perPage
     * @apiErrorExample 错误响应
     * {"code": 98040,"message": "errorMsg"}
     * @apiSuccessExample 成功响应
    {
    "code": 200,
    "data": {
    "current_page": 1,
    "data": [
        {
        "weeks": 202050,
        "rank":1,
        "period": 1,
        "type": 2,
        "head_picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/admin_images/5ee1d62377f10.jpeg",
        "nickname": "啊吧啊吧啊吧啊吧啊b",
        "total": 666666
        }
    ],
    "first_page_url": "http://127.0.0.1/week/star/history/user/list?=1",
    "from": 1,
    "last_page": 1,
    "last_page_url": "http://127.0.0.1/week/star/history/user/list?=1",
    "next_page_url": null,
    "path": "http://127.0.0.1/week/star/history/user/list",
    "per_page": 20,
    "prev_page_url": null,
    "to": 2,
    "total": 2
    }
    }
     */
    public function userRankInfoApi(Request $request)
    {
        $this_week = $request->input('this_week', 1);
        $type = $request->input('type', 0);
        $nickname = $request->input('nickname', "");
        $page = $request->input('page', 0);
        $perPage = $request->input('perPage', 0);
        $nowTime = time();
        if ($this_week ==1) {

            $weekList =  app('repo_week_star_history')->thisWeekStar($type);
        } else {
            $nYear = date('Y', $nowTime - 86400 * 7);
            $nWeek = date('W', $nowTime - 86400 * 7);
            $weeks = $nYear * 100 + $nWeek;
            $weekList = app('repo_week_star_history')->userRankInfo($weeks, $type);
            foreach ($weekList as &$week){
                if ($type==3){
                    $week['nickname']=$week['room_name'];
                    $week['head_picture']=$week['cover_picture'];
                    $week['room_label'] = app('model_room_label')->where('id', $week['label_id'])->value('label_name');
                    unset($week['room_name']);
                    unset($week['cover_picture']);
                    unset($week['sys_type_id']);
                }
            }
        }

        return Response::sendData($weekList);
    }

     /**
     * @api {get} week/star/history/rank/list 历史周星榜排名
     * @apiVersion 0.1.1
     * @apiName 获取历史周星榜排名信息
     * @apiGroup 周星榜管理
     *
     * @apiParam {string} nickname
     * @apiParam {number} page
     * @apiParam {number} perPage
     * @apiErrorExample 错误响应
     * {"code": 98040,"message": "errorMsg"}
     * @apiSuccessExample 成功响应
    {
    "code": 200,
    "data": {
    "current_page": 1,
    "data": [
    {
    "weeks": 202050,
    "period": 1,
    "type": 2,
    "head_picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/admin_images/5ee1d62377f10.jpeg",
    "nickname": "啊吧啊吧啊吧啊吧啊b",
    "total": 666666
    }
    ],
    "first_page_url": "http://127.0.0.1/week/star/history/rank/list?=1",
    "from": 1,
    "last_page": 1,
    "last_page_url": "http://127.0.0.1/week/star/history/rank/list?=1",
    "next_page_url": null,
    "path": "http://127.0.0.1/week/star/history/rank/list",
    "per_page": 20,
    "prev_page_url": null,
    "to": 2,
    "total": 2
    }
    }
     */
    public function getHistoryRankApi(Request $request)
    {
        $weekId = $request->input('week_id',0);
        $nickname = $request->input('nickname', "");
        $userCode = $request->input('user_code','');
        $type = $request->input('type',1);
        $page = $request->input('page', 0);
        $perPage = $request->input('perPage', 0);
        $weekList = app('repo_week_star_history')->getHistoryRank($weekId,$nickname,$userCode,$type,$page, $perPage);
        return Response::sendData($weekList);
    }


    /**
     *
     * @param Request $request
     * @return string
     * @throws \App\Exceptions\BasicException
     */
    public function exportHistory(Request $request)
    {
        $weekId = $request->input('week_id',0);
        $nickname = $request->input('nickname', "");
        $userCode = $request->input('user_code','');
        $type = $request->input('type',1);
        $weekList = app('repo_week_star_history')->getHistoryRank($weekId,$nickname,$userCode,$type,1, 10000);
        if ($type == 3){
            $headings = [
                '期数',
                '房间编号',
                '房间名称',
                '房间封面',
                '周星值',
                '房间标签',
            ];
        }else{
            $headings = [
                '期数',
                '用户编号',
                '用户昵称',
                '用户头像',
                '周星值',
            ];
        }
        $data = $weekList->toArray();
        $exportData = [];
        foreach ($data['data'] as $item){
            if ($type == 3){
                $temp = [
                    $item['week_star_id'],
                    $item['room_id'],
                    $item['nickname'],
                    $item['head_picture'],
                    $item['total'],
                    $item['room_label']
                ];
            }else{
                $temp = [
                    $item['week_star_id'],
                    $item['user_code'],
                    $item['nickname'],
                    $item['head_picture'],
                    $item['total'],
                ];
            }
            $exportData[] = $temp;
        }
        return app('repo_export')->export(35, $request->all(), $headings, $exportData);
    }
}

